Skip to content

Conversation

Pascalmh
Copy link
Contributor

onSubmitInvalid callback in FormApi is called when a submission attempt is made while canSubmit is false

Fixes #1696

Copy link

nx-cloud bot commented Aug 15, 2025

View your CI Pipeline Execution ↗ for commit cc883c4

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 10s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 25s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-30 12:39:10 UTC

Copy link

pkg-pr-new bot commented Aug 15, 2025

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1697

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1697

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@1697

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1697

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1697

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@1697

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1697

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1697

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1697

commit: cc883c4

Copy link

codecov bot commented Aug 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.48%. Comparing base (6892ed0) to head (cc883c4).
⚠️ Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1697      +/-   ##
==========================================
+ Coverage   90.35%   90.48%   +0.12%     
==========================================
  Files          38       38              
  Lines        1752     1786      +34     
  Branches      444      451       +7     
==========================================
+ Hits         1583     1616      +33     
- Misses        149      150       +1     
  Partials       20       20              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

formApi: this,
meta: submitMetaArg,
})
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this ensures onSubmitInvalid gets called, it happens before the submit validation. This means the formApi will have a stale error state when the callback is triggered.

@LeCarbonator
Copy link
Contributor

@kusiewicz It happens before the submit validation, but only in cases where canSubmit is false. That means there are currently errors present that have not been resolved, which is not stale.

If a user wanted to rerun validators even if canSubmit is false, they can use the canSubmitWhenInvalid property

@kusiewicz
Copy link
Contributor

kusiewicz commented Aug 16, 2025

@kusiewicz It happens before the submit validation, but only in cases where canSubmit is false. That means there are currently errors present that have not been resolved, which is not stale.

If a user wanted to rerun validators even if canSubmit is false, they can use the canSubmitWhenInvalid property

Ok i got it. I just thought that there:

  onSubmitInvalid: ({ formApi }) => {
    console.log(formApi.state.fieldMeta.xxx.errors);
  }

we should expect most recent errors, didn't know about canSubmitWhenInvalid

@LeCarbonator
Copy link
Contributor

well, they are the most recent errors. The most recent errors are preventing a submit altogether, so this PR asserts that attempting one should still trigger onSubmitInvalid

@jiji-hoon96
Copy link
Contributor

Hello @Pascalmh!

I encountered the same issue mentioned by @kusiewicz. Calling onSubmitInvalid before performing validation may result in outdated error states being passed to the callback.

Would you consider restructuring the logic to first perform the validation, check canSubmit, and then call onSubmitInvalid with the updated state? In this case, the user would receive the most recent validation error in the callback.

If it's okay with you, would it be possible for me to work on this and submit a PR?

@Pascalmh
Copy link
Contributor Author

@jiji-hoon96 sure, if you want you can PR against my fork and I'll merge your changes in so they appear in this PR here - or you can create a new PR, whatever you prefere 👍

@LeCarbonator
Copy link
Contributor

@jiji-hoon96 Are you sure it's stale? Even with canSubmitWhenInvalid: true?

@jiji-hoon96
Copy link
Contributor

Yes. I have confirmed its validity through testing. I may have made a mistake, so I would appreciate it if you could take a look at #1711 PR.

@jiji-hoon96
Copy link
Contributor

Pascalmh#5

I have submitted a pull request with some modifications to @Pascalmh`s work.

Copy link

changeset-bot bot commented Sep 30, 2025

🦋 Changeset detected

Latest commit: cc883c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/form-core Patch
@tanstack/angular-form Patch
@tanstack/form-devtools Patch
@tanstack/lit-form Patch
@tanstack/react-form Patch
@tanstack/solid-form Patch
@tanstack/svelte-form Patch
@tanstack/vue-form Patch
@tanstack/react-form-devtools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@LeCarbonator LeCarbonator changed the title fix(form-core): onSubmitInvalid not called when canSubmit false fix(form-core): call onSubmitInvalid even when canSubmit is false Sep 30, 2025
@LeCarbonator
Copy link
Contributor

This has been long overdue. Thanks for the fix!

@LeCarbonator LeCarbonator merged commit 2cfe44c into TanStack:main Sep 30, 2025
7 checks passed
@github-actions github-actions bot mentioned this pull request Sep 30, 2025
@Pascalmh Pascalmh deleted the fix-1696 branch September 30, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

form.onSubmitInvalid not called when canSubmit is false

4 participants